# import pandas as pd
#
# # 定义两个文件路径
# file_paths = [
#     r"C:\Users\20231112\Downloads\11.27\莨辰1.xlsx"
#
# ]
#
# # 存储数据框
# data_frames = []
#
# # 读取文件
# for path in file_paths:
#     try:
#         df = pd.read_excel(path)
#         data_frames.append(df)
#     except Exception as e:
#         print(f"读取文件 {path} 失败: {e}")
#
# # 合并所有数据
# if data_frames:
#     # 合并数据
#     combined_df = pd.concat(data_frames, ignore_index=True)
#
#     # 筛选日期为"全部"的记录
#     if '日期' in combined_df.columns:
#         filtered_df = combined_df[combined_df['日期'] == '全部']
#     else:
#         # 如果没有日期列，则使用所有数据
#         filtered_df = combined_df
#
#     # 只保留抖音号名称和抖音号ID两列（如果存在）
#     required_columns = ['抖音号名称', '抖音号ID']
#     existing_columns = [col for col in required_columns if col in filtered_df.columns]
#
#     if len(existing_columns) == 2:
#         result_df = filtered_df[required_columns].copy()
#
#         # 去重
#         result_df.drop_duplicates(inplace=True)
#
#         # 保存结果
#         result_df.to_excel(r'C:\Users\20231112\Downloads\11.27\投流消耗配置表\莨辰.xlsx', index=False)
#         print("合并、过滤、去重完成！结果已保存到莨辰.xlsx")
#     else:
#         print(f"所需的列不存在，现有列: {filtered_df.columns.tolist()}")
# else:
#     print("未找到任何有效文件。")

# V1.0

import pandas as pd
import os
from datetime import datetime

# Get current date in MM.DD format
current_date = datetime.now().strftime("%m.%d")

# Define file paths with current date
file_paths = [
    rf"C:\Users\20231112\Downloads\{current_date}\莨辰1.xlsx"
]

# Ensure output directory exists
output_dir = rf"C:\Users\20231112\Downloads\{current_date}\投流消耗配置表"
os.makedirs(output_dir, exist_ok=True)

# Store dataframes
data_frames = []

# Read files
for path in file_paths:
    try:
        df = pd.read_excel(path)
        data_frames.append(df)
    except Exception as e:
        print(f"读取文件 {path} 失败: {e}")

# Merge all data
if data_frames:
    # Merge data
    combined_df = pd.concat(data_frames, ignore_index=True)

    # Filter records where date is "全部"
    if '日期' in combined_df.columns:
        filtered_df = combined_df[combined_df['日期'] == '全部']
    else:
        # If no date column, use all data
        filtered_df = combined_df

    # Keep only douyin account name and ID columns (if they exist)
    required_columns = ['抖音号名称', '抖音号ID']
    existing_columns = [col for col in required_columns if col in filtered_df.columns]

    if len(existing_columns) == 2:
        result_df = filtered_df[required_columns].copy()

        # Remove duplicates
        result_df.drop_duplicates(inplace=True)

        # Save result
        result_df.to_excel(rf"{output_dir}\莨辰.xlsx", index=False)
        print("合并、过滤、去重完成！结果已保存到莨辰.xlsx")
    else:
        print(f"所需的列不存在，现有列: {filtered_df.columns.tolist()}")
else:
    print("未找到任何有效文件。")
